package com.gulimall.ware.service.impl;

import com.gulimall.common.utils.R;
import com.gulimall.ware.feign.ProductFeignService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;

import com.gulimall.ware.dao.PurchaseDetailDao;
import com.gulimall.ware.entity.PurchaseDetailEntity;
import com.gulimall.ware.service.PurchaseDetailService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {
    @Autowired
    private ProductFeignService productFeignService;

    /**
     * 采购单检索功能
     * key=&status=&wareId=
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();

        // 根据采购单id和 采购商品id进行检索
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("purchase_id", key).or().eq("sku_id", key);
            });
        }
        //根据状态进行检索
        String status = (String) params.get("status");
        if (StringUtils.isNotEmpty(status)) {
            queryWrapper.eq("status", status);
        }
        //根据仓库id进行检索
        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotEmpty(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }

        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 获取所有属于该采购单下的采购需求
     * @param purchaseId
     * @return
     */
    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long purchaseId) {
        return this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", purchaseId));
    }

    /**
     * 新增采购需求接口
     * @param purchaseDetail
     * @return
     */
    @Transactional
    @Override
    public R saveDetail(PurchaseDetailEntity purchaseDetail) {
        //调用远程接口查询sku详细信息
        R info = productFeignService.info(purchaseDetail.getSkuId());

        Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
        //如果取到的内容为空直接结束
        if (!data.isEmpty()){
            //获取商品单价
            Double price = (Double) data.get("price");
            //对采购金额进行计算 采购金额 = 采购数量 * 采购商品单价
            purchaseDetail.setSkuPrice(BigDecimal.valueOf(price).multiply(new BigDecimal(purchaseDetail.getSkuNum())));
            this.save(purchaseDetail);
            return R.ok();
        }
        return R.error("skuId不存在");
    }

    @Override
    public R updateDetailById(PurchaseDetailEntity purchaseDetail) {
        try {
            //调用远程接口查询sku详细信息
            R info = productFeignService.info(purchaseDetail.getSkuId());
            Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
            Double price = (Double) data.get("price");
            purchaseDetail.setSkuPrice(BigDecimal.valueOf(price));
            this.updateById(purchaseDetail);
            return R.ok();
        }catch (Exception e) {
            //如果取到的内容为空直接结束
            return R.error("skuId不存在");
        }
    }
}